<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('生产批次列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>批次号：</label>
                                <input type="text" name="batchNo"/>
                            </li>
                            <li>
                                <label>订单号：</label>
                                <input type="text" name="orderNo"/>
                            </li>
                            <li>
                                <label >销售订单编号：</label>
                                <input type="text" name="saleOrderNo"/>
                            </li>
                            <li>
                                <label>优先级：</label>
                                <select name="batchPriority" th:with="type=${@dict.getType('production_priority')}" required>
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.addTab()" shiro:hasPermission="order:productorder:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <!--<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="order:productorder:edit">-->
                    <!--<i class="fa fa-edit"></i> 修改-->
                <!--</a>-->
                <!--<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="order:productorder:remove">-->
                    <!--<i class="fa fa-remove"></i> 删除-->
                <!--</a>-->
                <a class="btn  btn-primary multiple" onclick="$.operate.audit()" shiro:hasPermission="order:productorder:audit">
                    <i class="fa fa-ticket"></i> 审核
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="order:productorder:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('order:productorder:edit')}]];
        var removeFlag = [[${@permission.hasPermi('order:productorder:remove')}]];
        var orderStatusDatas = [[${@dict.getType('order_status')}]];
        var productStatusDatas = [[${@dict.getType('production_status')}]];
        var deliveryStatusDatas = [[${@dict.getType('delivery_status')}]];
        var priorityLevelDatas = [[${@dict.getType('production_priority')}]];
        var unitDatas = [[${@dict.getType('product_unit')}]];
        var prefix = ctx + "order/productorder";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                auditUrl: prefix + "/audit",
                modalName: "生产订单",
                detailView: true,
                onExpandRow : function(index, row, $detail) {
                    initChildTable(index, row, $detail);
                },
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键ID',
                    visible: false
                },
                {
                    field: 'batchNo',
                    title: '批次号'
                },
                {
                    field: 'orderNo',
                    title: '订单号'
                },
                {
                    field: 'orderDate',
                    title: '订单时间'
                },
                {
                    field: 'saleOrderNo',
                    title: '销售订单编号'
                },
                {
                    field: 'goodsName',
                    title: '生产商品'
                },
                {
                    field: 'goodsNum',
                    title: '生产数量'
                },
                {
                    field: 'goodsUnitDesc',
                    title: '商品单位'
                },
                {
                    field: 'inNum',
                    title: '入库数量'
                },
                {
                    field: 'orderStatus',
                    title: '审核状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(orderStatusDatas, value);
                    }
                },
                {
                    field: 'productStatus',
                    title: '生产状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(productStatusDatas, value);
                    }
                },
                {
                    field: 'deliveryStatus',
                    title: '入库状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(deliveryStatusDatas, value);
                    }
                },
                {
                    field: 'batchPriority',
                    title: '生产优先级',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(priorityLevelDatas, value);
                    }
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(row.orderStatus==0) {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');

                        }
                        if(row.orderStatus==1){
                            if(row.productStatus==0) {
                                actions.push('<a class="btn btn-primary btn-xs " href="javascript:void(0)" onclick="productOutOrder(\'' + row.id + '\')"><i class="fa fa-ticket"></i>领料出库</a> ');
                            }else if(row.productStatus==1) {
                                actions.push('<a class="btn btn-primary btn-xs " href="javascript:void(0)" onclick="starting(\'' + row.id + '\')"><i class="fa fa-ticket"></i>开始生产</a> ');
                            }else if(row.productStatus==2) {
                                actions.push('<a class="btn btn-primary btn-xs " href="javascript:void(0)" onclick="ending(\'' + row.id + '\')"><i class="fa fa-ticket"></i>生产完成</a> ');
                            }
                        }
                        if(row.productStatus==3 && row.deliveryStatus==10){
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="productInstore(\'' + row.id + '\')"><i class="fa fa-inbox"></i>一键入库</a> ');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });


        initChildTable = function(index, row, $detail) {
            var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
            $(childTable).bootstrapTable({
                url: prefix + "/item/list",
                method: 'post',
                sidePagination: "server",
                contentType: "application/x-www-form-urlencoded",
                queryParams: {
                    mainId: row.id
                },
                columns: [
                    {
                        field: 'id',
                        title: 'id',
                        visible: false
                    },
                    {
                        field: 'goodsCode',
                        title: '商品编码'
                    },
                    {
                        field: 'goodsName',
                        title: '商品名称'
                    },
                    {
                        field: 'goodsUnit',
                        title: '单位',
                        formatter: function(value, row, index) {
                            return $.table.selectDictLabel(unitDatas, value);
                        }
                    },
                    {
                        field: 'planNumber',
                        title: '数量'
                    }
                ]

            });

        };



        function starting(id){
            $.modal.confirm("确认订单开始生产吗？", function () {
                $.operate.post(prefix + "/edit/status", {"id": id, "productStatus": '2'});

            })
        }

        function ending(id){
            $.modal.confirm("确认订单生产完成吗？", function () {
                $.operate.post(prefix + "/edit/status", {"id": id, "productStatus": '3'});

            })
        }

        function productInstore(id) {
            var preViewUrl=ctx+'store/inorder/productInstore?id='+id;
            $.modal.openTab("生产一键入库", preViewUrl);
        }

        function productOutOrder(id) {
            var preViewUrl=ctx+'store/outorder/productOutOrder?id='+id;
            $.modal.openTab("领料一键出库", preViewUrl);
        }
    </script>
</body>
</html>